package com.pshcp.doctor.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.announcement.DoctorAnnouncementPageAO;
import com.pshcp.doctor.ao.announcement.SaveDoctorAnnouncementAO;
import com.pshcp.doctor.ao.common.CommonAO;
import com.pshcp.doctor.tool.DoctorAnnouncementTool;
import com.pshcp.doctor.vo.announcement.DoctorAnnouncementVO;
import com.pshcp.domain.dto.DoctorAnnouncementDTO;
import com.pshcp.domain.model.DoctorAnnouncement;
import com.pshcp.service.DoctorAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 19:15
 */
@Api(tags = {"医生端-公告接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/announcement")
public class DoctorAnnouncementAPI {

    @Autowired
    private DoctorAnnouncementService doctorAnnouncementService;

    @ApiOperation("获取医生公告列表(分页)")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生公告列表(分页)")
    @PostMapping("getDoctorAnnouncementPage")
    public ResultDTO<PageList<DoctorAnnouncementVO>> getDoctorAnnouncementPage(@RequestBody DoctorAnnouncementPageAO doctorAnnouncementPageAO) throws BizException {
        DoctorAnnouncementDTO doctorAnnouncementDTO = DoctorAnnouncementTool.buildGetDoctorAnnouncementPage(doctorAnnouncementPageAO);
        PageList<DoctorAnnouncement> pageList = doctorAnnouncementService.list(doctorAnnouncementDTO);
        PageList<DoctorAnnouncementVO> page = CommonTool.buildPageList(pageList, DoctorAnnouncementVO.class);
        return ResultBuilder.success(page);
    }

    @ApiOperation("获取医生公告")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取医生公告")
    @PostMapping("getDoctorAnnouncementById")
    public ResultDTO<DoctorAnnouncementVO> getDoctorArticleById(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorAnnouncement doctorAnnouncement = doctorAnnouncementService.findById(commonAO.getId());
        CommonTool.checkObj(doctorAnnouncement, "90000007");
        DoctorAnnouncementVO doctorAnnouncementVO = CommonTool.copyProperties(doctorAnnouncement, DoctorAnnouncementVO.class);
        return ResultBuilder.success(doctorAnnouncementVO);
    }

    @ApiOperation("保存医生公告")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "保存医生公告")
    @PostMapping("saveDoctorAnnouncement")
    public ResultDTO<Long> saveDoctorAnnouncement(@RequestBody SaveDoctorAnnouncementAO saveDoctorAnnouncementAO) throws BizException {
        if (saveDoctorAnnouncementAO.getId() == null) {
            DoctorAnnouncement saveDoctorAnnouncement = DoctorAnnouncementTool.buildSaveDoctorAnnouncement(saveDoctorAnnouncementAO);
            doctorAnnouncementService.insert(saveDoctorAnnouncement);
        } else {
            DoctorAnnouncement doctorAnnouncement = doctorAnnouncementService.findById(saveDoctorAnnouncementAO.getId());
            CommonTool.checkObj(doctorAnnouncement, "90000007");
            DoctorAnnouncement updateDoctorAnnouncement = DoctorAnnouncementTool.buildSaveDoctorAnnouncement(doctorAnnouncement, saveDoctorAnnouncementAO);
            doctorAnnouncementService.update(updateDoctorAnnouncement);
        }
        return ResultBuilder.success(null);
    }

    @ApiOperation("删除医生公告")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.UPDATE, desc = "删除医生公告")
    @PostMapping("delDoctorAnnouncement")
    public ResultDTO<Long> delDoctorAnnouncement(@RequestBody CommonAO commonAO) throws BizException {
        CommonTool.checkObj(commonAO.getId(), "90000006");
        DoctorAnnouncement doctorAnnouncement = doctorAnnouncementService.findById(commonAO.getId());
        CommonTool.checkObj(doctorAnnouncement, "90000007");
        if (!doctorAnnouncement.getDoctorId().equals(ReqComTool.getUserId())){
            return ResultBuilder.failure("90000013");
        }
        doctorAnnouncementService.delById(commonAO.getId());
        return ResultBuilder.success(null);
    }
}
